
#ifndef _TTG_THREADS_WIN32_WIN32CRITiCALSECTIONS_
#define _TTG_THREADS_WIN32_WIN32CRITiCALSECTIONS_
#include "Common/Definitions.h"

#include "../CriticalSections.h"
#include "windows.h"

namespace ttg
{
	namespace threads
	{
		namespace win32
		{
			//Implementation of fast critical section.
			//So if it's busy, thread will be asking section in while(1) loop.
			class Win32FastCriticalSection :public ttg_internal::threads::FastCriticalSection
			{
				private:
					volatile LONG flag;		//section flag. 0 - empty, 1 - taken.
				public:
					//Creates configured untook section.
					Win32FastCriticalSection();
					//Tries to enter to section.
					//If it's busy, returns false, otherwise locks section and returns true.
					virtual bool tryEnter();
					//Locks resource.
					virtual void enter();
					//Unlocks resource.
					virtual void leave();
			};

			//Classic critical section (via mutex).
			class Win32SlowCriticalSection :public ttg_internal::threads::SlowCriticalSection
			{
				private:
					CRITICAL_SECTION cs;
				public:
					//Creates configured untook section.
					Win32SlowCriticalSection();
					//Tries to enter to section.
					//If it's busy, returns false, otherwise locks section and returns true.
					virtual bool tryEnter();
					//Locks resource.
					virtual void enter();
					//Unlocks resource.
					virtual void leave();
					~Win32SlowCriticalSection();
			};
		}
	}
}

namespace ttg
{
	namespace threads
	{
		typedef win32::Win32FastCriticalSection FastCriticalSection;
		typedef win32::Win32SlowCriticalSection SlowCriticalSection;
	}
}

#endif
